<?php
/**
 * Created by PhpStorm.
 * User: dt
 * Time: 11:44
 */
namespace backend\base;
/**
 * 模型基类
 * @package app\base
 */
use Yii;
use vod\Request\V20170321 as vod;
class BaseModel extends \yii\db\ActiveRecord
{
    public static $del_img_list = [];
    private static $_instances =[];
    public static $regionId = 'cn-shanghai';

    /**
     * 保存后
     */
    public function afterSave($insert, $changedAttributes)
    {
//        //删除旧图片
//        if(self::$del_img_list){
//            $oss = new \backend\models\OssModel();
//            if($oss){
//                $oss->dele(self::$del_img_list);
//                self::$del_img_list = [];
//            }
//        }
        parent::afterSave($insert, $changedAttributes);
    }

    /**
     * 时间戳转时间
     * @return string
     */
    public function getFormatDateTime(){
        $time = isset($this->create_time) ? $this->create_time : $this->created_at;
        return date('Y-m-d H:i:s',$time);
    }
    /**
     * 时间戳转日期
     * @return string
     */
    public function getFormatDate(){
        $time = isset($this->create_time) ? $this->create_time : $this->created_at;
        return date('Y-m-d',$time);
    }

    /**
     * 验证手机号码
     */
    public static function testMobile($mobile)
    {
        $test = preg_match("/^1[34578]{1}\d{9}$/",$mobile);
        if ($test) {
            return $test;
        } else {
            return false;
        }
    }

    /**
     * 验证密码 数字英文 6-14位
     */
    public static function testPassWord($mobile)
    {
        $test = preg_match("/^[a-zA-Z0-9]{6,10}$/",$mobile);
        if ($test) {
            return $test;
        } else {
            return false;
        }
    }

    /**
     * 验证性别类型 1-3
     */
    public static function testSex($deviceId)
    {
        if($deviceId==1 || $deviceId==2 || $deviceId==3){
            return true;
        }
        return false;
    }

    /**
     * 生成不重复的随机数
     * @param  int $start  需要生成的数字开始范围
     * @param  int $end    结束范围
     * @param  int $length 需要生成的随机数个数
     * @return array       生成的随机数
     */
    public static function get_rand_number($start=100000,$end=999999,$length=6){
        $connt=0;
        $temp='';
        while($connt<$length){
            $temp.=mt_rand($start,$end);
            return $temp;
        }
    }

    /**
     * 单例初始化model
     * @param string $className
     * @return mixed
     */
    public static function getInstance($className = __CLASS__ )
    {
        if (empty(self::$_instances[$className])) {
            self::$_instances[$className] = new $className(null, null, []);
        }
        return self::$_instances[$className];
    }



    /**
     * 获取视频播放信息
     * @return string
     */
    public static function vetPlayInfo($data){
        try{
            if(!isset($data['video_id']) || !$data['video_id']){
                return '';
            }
            $client = self::getVideoClient();
            $request = new vod\GetPlayInfoRequest();
            $request->setVideoId($data['video_id']);
            $request->setFormats("mp4");
            $response =$client->getAcsResponse($request);
            if(!isset($response->PlayInfoList->PlayInfo[0])){
                return '';
            }
            return $response->PlayInfoList->PlayInfo[0];
        }catch(\Exception $e){
            var_dump($e->getMessage());
            return '';
        }
    }

    /**
     * 获取视频信息
     * @param $data
     * @return string
     */
    public static function getVideoInfo($data){
        try{
            if(!isset($data['video_id']) || !$data['video_id']){
                return '';
            }
            $client = self::getVideoClient();
            $request = new vod\GetVideoInfoRequest();
            $request->setVideoId($data['video_id']);
            $response =$client->getAcsResponse($request);
            if(!isset($response->Video)){
                return '';
            }
            return $response->Video;
        }catch(\Exception $e){
            return '';
        }
    }

    /**
     * 视频客户端
     * @return \DefaultAcsClient
     */
    public static function getVideoClient(){
        include_once dirname(dirname(__DIR__)) . '/backend/utils/aliyun-php-sdk-core/Config.php';
        $profile = \DefaultProfile::getProfile(self::$regionId, Yii::$app->params['oss']['key'],Yii::$app->params['oss']['secret']);
        return new \DefaultAcsClient($profile);
    }



    /**
     * 秒数转天时分秒
     * @param $seconds
     * @return string
     */
    public static function secToTime($time){
        if(is_numeric($time)){
            $str = '';
            if($time >= 31556926){
                $str .= floor($time/31556926).'年';
                $time = ($time%31556926);
            }
            if($time >= 86400){
                $str .= floor($time/86400).'天';
                $time = ($time%86400);
            }
            if($time >= 3600){
                $str .= floor($time/3600).'小时';
                $time = ($time%3600);
            }
            if($time >= 60){
                $str .= floor($time/60).'分';
                $time = ($time%60);
            }
            $str .= floor($time).'秒';
            Return $str;

        }else{
            return '0秒';
        }
    }

    /**
     * 大小单位转换
     * @param $size
     * @return string
     */
    public static function formatBytes($size) {
        $units = array(' B', ' KB', ' MB', ' GB', ' TB');
        for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
        return round($size, 2).$units[$i];
    }
}
